package com.example.emqx.util;

import com.example.emqx.entity.DevicesEmqAll;
import com.example.emqx.entity.DevicesEmqPayload;
import com.example.emqx.entity.DevicesEmqStatus;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Date;
import java.util.Map;

public class EmqUtil {
    public static String getMapString(String key, Map<String, Object> map) {
        if (!map.containsKey(key)) {
            return "";
        }
        return map.get(key).toString();
    }

    public static Long getMapLong(String key, Map<String, Object> map) {
        if (!map.containsKey(key)) {
            return 0L;
        }
        return (Long) map.get(key);
    }

    public static int getMapInt(String key, Map<String, Object> map) {
        if (!map.containsKey(key)) {
            return 0;
        }
        return (int) map.get(key);
    }

    public static DevicesEmqAll getDevicesEmq(Map<String, Object> map) {
        return DevicesEmqAll.builder()
                .protoName(EmqUtil.getMapString("proto_name", map))
                .clientId(EmqUtil.getMapString("clientid", map))
                .isBridge(EmqUtil.getMapString("is_bridge", map))
                .mountPoint(EmqUtil.getMapString("mountpoint", map))
                .keepalive(EmqUtil.getMapInt("keepalive", map))
                .receiveMaximum(EmqUtil.getMapInt("receive_maximum", map))
                .userName(EmqUtil.getMapString("username", map))
                .node(EmqUtil.getMapString("node", map))
                .sockname(EmqUtil.getMapString("sockname", map))
                .peername(EmqUtil.getMapString("peername", map))
                .event(EmqUtil.getMapString("event", map))
                .msgId(EmqUtil.getMapString("id", map))
                .time(new Date())
                .build();
    }

    public static DevicesEmqStatus getDevicesEmqStatus(Map<String, Object> map) {
        return DevicesEmqStatus.builder()
                .protoName(EmqUtil.getMapString("proto_name", map))
                .clientId(EmqUtil.getMapString("clientid", map))
                .isBridge(EmqUtil.getMapString("is_bridge", map))
                .mountPoint(EmqUtil.getMapString("mountpoint", map))
                .keepalive(EmqUtil.getMapInt("keepalive", map))
                .receiveMaximum(EmqUtil.getMapInt("receive_maximum", map))
                .userName(EmqUtil.getMapString("username", map))
                .node(EmqUtil.getMapString("node", map))
                .sockname(EmqUtil.getMapString("sockname", map))
                .peername(EmqUtil.getMapString("peername", map))
                .event(EmqUtil.getMapString("event", map))
                .time(new Date())
                .build();
    }

    public static DevicesEmqPayload jsonToDevicesEmqPayload(ObjectMapper objectMapper,String jsonString) throws Exception {
        try {
            JsonNode jsonNode = objectMapper.readTree(jsonString);
            String payload = jsonNode.get("payload").asText();
            JsonNode payloadNode = objectMapper.readTree(payload);
            String msg = "";
            msg = payloadNode.get("msg").asText();
            return DevicesEmqPayload.builder()
                    .msg(msg)
                    .msgId(jsonNode.get("id").asText()) // Use id as msgId
                    .qos(jsonNode.get("qos").asText())
                    .topic(jsonNode.get("topic").asText())
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            return DevicesEmqPayload.builder()
                    .msgId(jsonString).build();
        }


    }
}
